package com.ysx.oa.role.controller;

import com.ysx.oa.role.bean.RoleBean;
import com.ysx.oa.role.bean.UserRoleBean;
import com.ysx.oa.role.dto.RoleDTO;
import com.ysx.oa.role.dto.UserRoleDTO;
import com.ysx.oa.role.service.RoleService;
import com.ysx.oa.role.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 王伟雄
 * @Date: 2021/11/20 17:18
 */


@RestController
@RequestMapping("/userRole")
public class UserRoleController {
    @Autowired
    private UserRoleService service;

    @Autowired
    private RoleService roleService;


    // http://localhost:8080/kaoqin/userRole/add.do
    @CrossOrigin
    @RequestMapping(value="/add.do",method = RequestMethod.POST)
    public Map<String,Object> add(@RequestBody UserRoleBean bean){
        Map<String,Object> map = new HashMap();

        Integer rs = service.add(bean);
        if(rs==0){
            map.put("code",500);
            map.put("message","failure");
        }else {
            map.put("code",200);
            map.put("message","success");
        }

        return map;
    }

    // http://localhost:8080/kaoqin/userRole/update.do
    @RequestMapping(value="/update.do",method = RequestMethod.POST)
    @CrossOrigin
    public Map<String,Object> update(@RequestBody UserRoleBean bean){
        Map<String,Object> map = new HashMap();

        Integer rs = service.update(bean);

        if(rs==0){
            map.put("code",500);
            map.put("message","failure");
        }else {
            map.put("code",200);
            map.put("message","success");
        }
        return map;
    }

    // http://localhost:8080/kaoqin/userRole/find.do
    @RequestMapping(value="/findSelected.do",method = RequestMethod.POST)
    @CrossOrigin
    public Map<String,Object> findSelected(@RequestBody UserRoleBean bean){
        Map<String,Object> map = new HashMap();
        List<UserRoleBean> data = service.findSelected(bean);

        map.put("code",200);
        map.put("message","success");
        map.put("data",data);
        return map;
    }

    // http://localhost:8080/kaoqin/userRole/findAll.do
    @RequestMapping(value="/findAll.do",method = RequestMethod.GET)
    @CrossOrigin
    public Map<String,Object> findAll(){
        Map<String,Object> map = new HashMap();
        List<UserRoleBean> userRoleBeans = service.findAll();
        List<RoleBean> roleBeans = roleService.findAllRole();

        map.put("code",200);
        map.put("message","success");
        map.put("userRoleBeans",userRoleBeans);
        map.put("roleBeans",roleBeans);
        return map;
    }

    // http://localhost:8080/kaoqin/userRole/findAllAllocated.do
    @RequestMapping(value="/findAllAllocated.do",method = RequestMethod.GET)
    @CrossOrigin
    public Map<String,Object> findAllAllocated(){
        Map<String,Object> map = new HashMap();
        List<UserRoleBean> data = service.findAllAllocated();

        map.put("code",200);
        map.put("message","success");
        map.put("data",data);
        return map;
    }

    // http://localhost:8080/kaoqin/userRole/delete.do
    @RequestMapping(value = "/delete.do",method = RequestMethod.POST)
    @CrossOrigin
    public Map<String,Object> delete(@RequestBody UserRoleDTO userRoleDTO){
        Map<String,Object> map = new HashMap();
        Integer delete = service.delete(userRoleDTO);
        if(delete>0){
            map.put("code",200);
            map.put("message","success");
        }else {
            map.put("code",500);
            map.put("message","用户已分配班级");
        }
        return map;
    }
}
